package com.habitracker.view;

import com.habitracker.controller.DataManager;
import com.habitracker.model.Goal;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.layout.*;

import java.util.List;

public class ProgressPage {
    private DataManager dataManager;
    private VBox mainLayout;

    public ProgressPage(DataManager dataManager) {
        this.dataManager = dataManager;
        this.mainLayout = createMainLayout();
    }

    public VBox getLayout() { return mainLayout; }

    private VBox createMainLayout() {
        VBox mainLayout = new VBox(20);
        mainLayout.setPadding(new Insets(20));
        mainLayout.setPrefSize(800, 600);
        mainLayout.setStyle("-fx-background-color: #ffffff;");

        // 标题
        Label title = new Label("目标进度");
        title.setStyle("-fx-font-size: 24px; -fx-font-weight: bold; -fx-text-fill: #333333;");

        // 创建完全横向的目标区域
        HBox horizontalContainer = createHorizontalContainer();

        mainLayout.getChildren().addAll(title, horizontalContainer);
        return mainLayout;
    }

    private HBox createHorizontalContainer() {
        HBox container = new HBox(30); // 分类之间的间距
        container.setPadding(new Insets(20));
        container.setAlignment(Pos.TOP_CENTER);

        // 获取所有分类
        List<String> categories = dataManager.getGoalCategories();

        for (String category : categories) {
            // 为每个分类创建垂直区域
            VBox categoryColumn = createCategoryColumn(category);
            container.getChildren().add(categoryColumn);
        }

        return container;
    }

    private VBox createCategoryColumn(String category) {
        VBox categoryColumn = new VBox(15);
        categoryColumn.setAlignment(Pos.TOP_CENTER);
        categoryColumn.setPrefWidth(200);

        // 分类标题
        Label categoryTitle = new Label(category);
        categoryTitle.setStyle("-fx-font-size: 20px; -fx-font-weight: bold; -fx-text-fill: #333333;");

        // 该分类的所有目标卡片
        VBox goalsColumn = new VBox(15);
        goalsColumn.setAlignment(Pos.TOP_CENTER);

        // 获取该分类的所有目标
        List<Goal> categoryGoals = dataManager.getGoalsByCategory(category);
        for (Goal goal : categoryGoals) {
            VBox goalCard = createGoalCard(goal);
            goalsColumn.getChildren().add(goalCard);
        }

        // 添加目标按钮
        Button addButton = createAddButton(category, goalsColumn);

        categoryColumn.getChildren().addAll(categoryTitle, goalsColumn, addButton);
        return categoryColumn;
    }

    private VBox createGoalCard(Goal goal) {
        VBox card = new VBox(8);
        card.setPadding(new Insets(12));
        card.setAlignment(Pos.CENTER);
        card.setPrefSize(160, 100);
        card.setStyle("-fx-background-color: #f8f9fa; -fx-border-color: #e0e0e0; -fx-border-radius: 8; " +
                "-fx-background-radius: 8;");

        // 可编辑的目标名称
        TextField nameField = createNameTextField(goal);

        // 进度显示和调整区域
        HBox progressArea = createProgressArea(goal);

        card.getChildren().addAll(nameField, progressArea);
        return card;
    }

    private TextField createNameTextField(Goal goal) {
        TextField nameField = new TextField(goal.getName());
        nameField.setStyle("-fx-font-size: 14px; -fx-font-weight: bold; -fx-text-fill: #333333; " +
                "-fx-background-color: transparent; -fx-border-color: transparent; " +
                "-fx-alignment: center;");
        nameField.setPrefWidth(120);
        nameField.setMaxWidth(120);

        if ("请输入".equals(goal.getName())) {
            nameField.setStyle("-fx-font-size: 14px; -fx-text-fill: #999999; -fx-font-style: italic; " +
                    "-fx-background-color: transparent; -fx-border-color: transparent; " +
                    "-fx-alignment: center;");
        }

        nameField.focusedProperty().addListener((obs, oldVal, newVal) -> {
            if (newVal) {
                if ("请输入".equals(nameField.getText())) {
                    nameField.setText("");
                }
                nameField.setStyle("-fx-font-size: 14px; -fx-font-weight: bold; -fx-text-fill: #333333; " +
                        "-fx-background-color: #ffffff; -fx-border-color: #2196F3; " +
                        "-fx-alignment: center;");
            } else {
                if (nameField.getText().trim().isEmpty()) {
                    nameField.setText("请输入");
                    nameField.setStyle("-fx-font-size: 14px; -fx-text-fill: #999999; -fx-font-style: italic; " +
                            "-fx-background-color: transparent; -fx-border-color: transparent; " +
                            "-fx-alignment: center;");
                } else {
                    nameField.setStyle("-fx-font-size: 14px; -fx-font-weight: bold; -fx-text-fill: #333333; " +
                            "-fx-background-color: transparent; -fx-border-color: transparent; " +
                            "-fx-alignment: center;");
                    goal.setName(nameField.getText().trim());
                }
            }
        });

        return nameField;
    }

    private HBox createProgressArea(Goal goal) {
        HBox progressArea = new HBox(8);
        progressArea.setAlignment(Pos.CENTER);

        // 进度显示
        Label progressLabel = new Label(goal.getProgressDisplay());
        progressLabel.setStyle("-fx-font-size: 14px; -fx-text-fill: #666666;");
        progressLabel.setPrefWidth(40);

        // 进度调整按钮
        Button decreaseBtn = new Button("-");
        decreaseBtn.setStyle("-fx-background-color: #ff4444; -fx-text-fill: white; -fx-font-weight: bold; " +
                "-fx-min-width: 20px; -fx-min-height: 20px; -fx-font-size: 10px;");

        Button increaseBtn = new Button("+");
        increaseBtn.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-weight: bold; " +
                "-fx-min-width: 20px; -fx-min-height: 20px; -fx-font-size: 10px;");

        // 按钮事件
        decreaseBtn.setOnAction(e -> {
            double newProgress = Math.max(0, goal.getProgress() - 0.1);
            goal.setProgress(newProgress);
            progressLabel.setText(goal.getProgressDisplay());
        });

        increaseBtn.setOnAction(e -> {
            double newProgress = Math.min(1, goal.getProgress() + 0.1);
            goal.setProgress(newProgress);
            progressLabel.setText(goal.getProgressDisplay());
        });

        progressArea.getChildren().addAll(decreaseBtn, progressLabel, increaseBtn);
        return progressArea;
    }

    private Button createAddButton(String category, VBox goalsColumn) {
        Button addButton = new Button("+");
        addButton.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white; -fx-font-weight: bold; " +
                "-fx-font-size: 16px; -fx-min-width: 40px; -fx-min-height: 40px; " +
                "-fx-background-radius: 20;");

        addButton.setOnAction(e -> {
            // 创建新目标
            Goal newGoal = new Goal("新目标", category, 0.0);
            dataManager.addGoal(newGoal);

            // 创建新卡片并添加到列中
            VBox newGoalCard = createGoalCard(newGoal);
            goalsColumn.getChildren().add(newGoalCard);
        });

        return addButton;
    }
}